<script type="text/javascript">
$(function(){
	var pg = App.batch.executionIndex.ui, pp = pg.params;
	
	var gd = pg.find('.grid').mac('grid', {
		key: 'id',
		cols: [{
			name: 'no', title : '&nbsp;', width: 18,
			render: function(r, tr, me) {
				var a = $('<span class="icon icon-plus"></span>');
				a.click(function(){
					var yy = me.find('.ybar>div'), pl = tr.exPanel;
					if (a.hasClass('icon-plus')) {
						if (pl) {
							pl.show();
						} else {
							pl = $('<div class="expand"></div>');
							pl.append('&nbsp;执行序号：' + r.no + '<br>');
							pl.append('&nbsp;运行参数：' + r.params + '<br>');
							pl.append('&nbsp;报错信息：' + r.error);
							tr.exPanel = pl.insertAfter(tr);
							$('<div class="clear"></div>').insertBefore(pl);
						}
						yy.height(yy.height() + pl.height());
						a.removeClass('icon-plus').addClass('icon-minus');
					} else {
						pl.hide();
						yy.height(yy.height() - pl.height());
						a.removeClass('icon-minus').addClass('icon-plus');
					}
				});
				return a;
			}
		},{
			field: 'type', title: '类型', width: 60,
			render: function(r){
				return Cache.batch.execution.type[r.type];
			}
		},{
			field: 'addr', title: '执行节点', width: 120
		},{
			field: 'startTime', title: '开始时间', width: 160
		},{
			field: 'endTime', title: '结束时间', width: 160
		},{
			field: 'status', title: '进度与状态', width: 120,
			render: function(r){
				return Cache.batch.execution.status[r.status];
			}
		},{
			field: 'id', title : '操作', width: 200,
			render: function(r, tr, gd) {
				var p = $('<div></div>'), v = r.status,
					st = '<span class="a"></span>';
				//只有失败4、挂起5或中断7的任务组执行才可以续跑
				if (App.logon.power('batch.execution.resume')
					&& (v == 4 || v == 5 || v == 7)) {
					$(st).click(function(){
						$.post(App.ctx + '/dispatcher/resume.do', {
							id: r.id,
						}, function(rs){
							gd.load();
						});
					}).append('续跑').appendTo(p);
					p.append('&nbsp;');
				}
				//只有成功3、失败4、挂起5或中断7的批量任务才可以重跑
				if (App.logon.power('batch.execution.redo')
					&& (v == 3 || v == 4 || v == 5 || v == 7)) {
					$(st).click(function(){
						$.post(App.ctx + '/dispatcher/redo.do', {
							id: r.id,
						}, function(rs){
							gd.load();
						});
					}).append('重跑').appendTo(p);
					p.append('&nbsp;');
				}
				//只有初始0、排队1或执行2的任务组执行才可以取消
				if (App.logon.power('batch.execution.cancel')
					&& (v == 0 || v == 1 || v == 2)) {
					$(st).click(function(){
						$.post(App.ctx + '/dispatcher/cancel.do', {
							id: r.id,
						}, function(rs){
							gd.load();
						});
					}).append('取消').appendTo(p);
					p.append('&nbsp;');
				}
				return p;
			}
		}],
		loader: {
			url: App.ctx + '/execution/list.do',
			params: {
				infoId: pp.id, pageNo: 1, pageSize: 20
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	
	pg.seek('key').keydown(function(e) {
		if (e.keyCode == 13) {
			gd.load({ key : $(this).val() });
		}
	});
	pg.seek('back').click(function(){
		App.show({
			ctx : 'batch',
			action : 'infoIndex',
			params : {}
		}, $.noop, pg.parent());
	});
	pg.seek('refresh').click(function(){
		gd.load();
	});
	
	function adjust(){
		var p = pg.parent(), w = p.width(), h = p.height();
		pg.width(w - 64);
		gd.parent().height(h - 96);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust();
	
	pg.children('.head').seek('title').text(pp.name);
	
	function refreshStatus() {
		var a = $.map(gd.data,function(o,k){
			if ($.inArray(o.status,[3,4,5,7,8])<0){
				return o.id;
			}
		});
		if(a.length==0)
			return;
		$.post(App.ctx + '/execution/refresh.do', {
			key: a.join(',')
		}, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				$.map(ro.data,function(o,k){
					pg.find("div[name='"+o.id+"'] div[name='status']").html(Cache.batch.execution.status[o.status]);
				});
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}

		});
	}
	App.timer = setInterval(function () {refreshStatus()},5000);
});
</script>
<div class="navi">
	<span class="a" name="back">批量列表</span>&gt;&gt;
	<span name="title"></span>&nbsp;执行记录
</div>
<div class="head">
	<input type="text" name="key" class="fr" placeholder="查找" />
	<button name="refresh">手动刷新</button>
</div>
<div class="body">
	<div class="grid"></div>
</div>